package com.kexin.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.show}")
    private boolean swaggerShow;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .enable(swaggerShow)
            .pathMapping("/")
            .globalOperationParameters(setHeaderToken())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.kexin"))
            .paths(PathSelectors.any())
            .build().apiInfo(new ApiInfoBuilder()
                    .title("数字生活——卡券接口")
                    .description("卡券查询及购买")
                    .build());
    }

    private List<Parameter> setHeaderToken() {
        ParameterBuilder appToken = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        appToken.name("X-Token").description("app登录token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(appToken.build());
        return pars;
    }
}
